package com.cenntro.oss.service.impl;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.cenntro.auth.mapper.SysSooidMapper;
import com.cenntro.auth.model.dbo.SysSooidDO;
import com.cenntro.common.aliyun.FileUpload;
import com.cenntro.common.base.BaseServiceImpl;
import com.cenntro.common.util.MessageDigestUtil;
import com.cenntro.common.util.result.Result;
import com.cenntro.oss.service.SysSooidService;
import com.google.common.collect.ImmutableMap;

import lombok.extern.log4j.Log4j;

/**
 * <p>
 * 账号数据表 服务实现类
 * </p>
 *
 * @author
 * @since 2019-03-21
 */
@Log4j
@Service
public class SysSooidServiceImpl extends BaseServiceImpl<SysSooidMapper> implements SysSooidService {

	@Resource
	private SysSooidMapper sysSooidMapper;

	/**
	 * 修改密码
	 */
	@Override
	public Integer updateNewPassword(String newPassword, String userName) {
		SysSooidDO sooidDO = sysSooidMapper.selectOne(ImmutableMap.of("userName", userName));
		if (sooidDO != null) {
			//加密MD5
			sooidDO.setPassword(MessageDigestUtil.MD5(newPassword));
//			sooidDO.setPassword(newPassword);
			int update = sysSooidMapper.update(sooidDO);
			if (update > 0) {
				return 1;
			} else {
				return -1;
			}
		} else {
			return -1;
		}
	}

	@Override
	public Result updateHeadPic(String userName, HttpServletRequest request) {
		SysSooidDO sooidDO = sysSooidMapper.selectOne(ImmutableMap.of("userName", userName));
		String headPic = null;
		if (sooidDO != null) {
			FileUpload file = new FileUpload();
			try {
				headPic = file.upload(request);
				sooidDO.setHeadPic(headPic);
				sysSooidMapper.update(sooidDO);
				file.deleteFile(sooidDO.getHeadPic());
			} catch (Exception e) {
				e.printStackTrace();
				log.error(e);
			}
		}
		return Result.ok(headPic);
	}

}
